package cn.sanenen.dm.base;

import cn.hutool.log.Log;
import cn.sanenen.dm.common.Constant;

import java.util.concurrent.ConcurrentHashMap;

public class DmApiCache {
    private static final Log log = Log.get();

    private static ConcurrentHashMap<Long,DmApi> dmApiMap = new ConcurrentHashMap<>();



    public static void releaseInstance(long hwnd) {
        DmApi dm = dmApiMap.remove(hwnd);
        if (dm != null) {
            dm.UnBindWindow();
        }
    }

    public static DmApi createNewInstance(long hwdn) {
        if(dmApiMap.containsKey(hwdn)) {
            return dmApiMap.get(hwdn);
        }
       DmApi instance = DMRegHandler.newDmObject(DmApi.class);
        String classpathRoot = ClassLoader.getSystemResource("").getPath();
        if (classpathRoot.startsWith("/") && classpathRoot.contains(":")) {
            classpathRoot = classpathRoot.substring(1); // 去掉开头的 /
        }
        instance.SetPath(classpathRoot + Constant.DM_FILES);
        long reg = instance.Reg("826980bda1470e5da3819d63d9c432b16162d8", "");
        if (reg == 1) {
            log.info(hwdn+"初始化大漠插件成功");
        }
        dmApiMap.put(hwdn, instance);
        return instance;
    }

    public static DmApi createNewInstance2(long hwdn) {
        if(dmApiMap.containsKey(hwdn)) {
            return dmApiMap.get(hwdn);
        }
        DmApi instance = DMRegHandler.newDmObject(DmApi.class);

        instance.SetPath(Constant.DM_FILES);
        long reg = instance.Reg("826980bda1470e5da3819d63d9c432b16162d8", "");
        if (reg == 1) {
            log.info(hwdn+"初始化大漠插件成功");
        }
        dmApiMap.put(hwdn, instance);
        return instance;
    }
}
